package com.lewis.module.m2.api.integral.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.utils.DateUtil;
import com.lewis.module.m2.api.integral.entity.ImmortalIntegral;
import com.lewis.module.m2.api.integral.mapper.ImmortalIntegralMapper;
import com.lewis.module.m2.api.integral.service.IImmortalIntegralService;
import com.lewis.module.m2.api.integral.vo.ImmortalVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 修仙配置表 服务实现类
 * </p>
 *
 * @author lewis
 * @since 2021-11-23
 */
@Service
public class ImmortalIntegralServiceImpl extends ServiceImpl<ImmortalIntegralMapper, ImmortalIntegral> implements IImmortalIntegralService {

	@Resource
	private ImmortalIntegralMapper immortalIntegralMapper;

	@Override
	public BaseResult immortal(ImmortalVo vo) {
		// 查询数据
		ImmortalIntegral immortalIntegral = this.getByQq(vo.getQq(), vo.getGroupId());
		if (immortalIntegral == null) {
			// 不存在则直接创建
			immortalIntegral = new ImmortalIntegral();
			immortalIntegral.setQq(String.valueOf(vo.getQq()));
			immortalIntegral.setGroupId(String.valueOf(vo.getGroupId()));
			if (immortalIntegralMapper.insert(immortalIntegral) != 1) {
				return BaseResult.fail().setMsg("修仙失败，请重试！");
			}
		}
		Integer rank = this.getRankByQq(vo.getQq(), vo.getGroupId());
		// 确保今日数据未修改
		if (this.existTodayData(vo.getQq(), vo.getGroupId())) {
			return BaseResult.fail().setMsg("修仙失败，今天已经修过仙了！\n" +
					"当前仙位：" + rank + "\n" +
					"当前仙力：" + immortalIntegral.getIntegral());
		}
		List<String> msgList = new ArrayList<>();
		// 随机积分（1~99）
		Random random = new Random();
		int addRandom = random.nextInt(99);
		immortalIntegral.setIntegral(immortalIntegral.getIntegral() + addRandom);
		immortalIntegral.setUtime(DateUtil.getTime());
		// 更新
		if (immortalIntegralMapper.updateById(immortalIntegral) == 1) {
			msgList.add("修仙成功！获得仙力：" + addRandom + "\n" +
					"当前仙位：" + rank + "\n" +
					"当前仙力：" + immortalIntegral.getIntegral());
		} else {
			msgList.add("修仙失败，请重试！");
		}
		return BaseResult.ok().setData(msgList);
	}

	@Override
	public ImmortalIntegral getByQq(String qq, String groupId) {
		LambdaQueryWrapper<ImmortalIntegral> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ImmortalIntegral::getQq, qq);
		queryWrapper.eq(ImmortalIntegral::getGroupId, groupId);
		return immortalIntegralMapper.selectOne(queryWrapper);
	}

	@Override
	public Integer getRankByQq(String qq, String groupId) {
		return immortalIntegralMapper.getRankByQq(qq, groupId);
	}

	@Override
	public BaseResult get2TenRank(String groupId) {
		return BaseResult.ok().setData(immortalIntegralMapper.get2TenRank(groupId));
	}

	@Override
	public boolean existTodayData(String qq, String groupId) {
		String date = DateUtil.getDate();
		LambdaQueryWrapper<ImmortalIntegral> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ImmortalIntegral::getQq, qq);
		queryWrapper.eq(ImmortalIntegral::getGroupId, groupId);
		queryWrapper.like(ImmortalIntegral::getUtime, date);
		List<ImmortalIntegral> list = immortalIntegralMapper.selectList(queryWrapper);
		return list != null && list.size() != 0;
	}

	@Override
	public boolean existQqInGroupData(String qq, String groupId) {
		LambdaQueryWrapper<ImmortalIntegral> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ImmortalIntegral::getQq, qq);
		queryWrapper.eq(ImmortalIntegral::getGroupId, groupId);
		List<ImmortalIntegral> list = immortalIntegralMapper.selectList(queryWrapper);
		return list != null && list.size() != 0;
	}
}
